package com.situ.datasave;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.id;

public class DatabaseActivity extends AppCompatActivity {
    private Button createDatabaseButton;
    private Button addDataButton;
    private Button queryDataButton;
    private Button updateDataButton;
    private Button deleteDataButton;


    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_database);

        // dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);
        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
        createDatabaseButton = (Button) findViewById(R.id.create_database);
        createDatabaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });

        addDataButton = (Button) findViewById(R.id.add_data);
        addDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                // 开始组装提一条数据
                values.put("name", "Tom");
                values.put("author", "ABC");
                values.put("pages", "23");
                values.put("price", "100");
                db.insert("Book", null, values);
                values.clear();
                // 开始组装提二条数据
                values.put("name", "Tom2");
                values.put("author", "ABC2");
                values.put("pages", "232");
                values.put("price", "1002");
                db.insert("Book", null, values);
                // insert into Book() values();
            }
        });

        // 查询数据
        queryDataButton = (Button) findViewById(R.id.query_data);
        queryDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                List<Book> list = new ArrayList<Book>();
                if (cursor.moveToFirst()) {
                    do {
                       int id = cursor.getInt(cursor.getColumnIndex("id"));
                       String name = cursor.getString(cursor.getColumnIndex("name"));
                       String author = cursor.getString(cursor.getColumnIndex("author"));
                       int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                       double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Book book = new Book(id, name, author, pages, price );
                        list.add(book);
                    } while (cursor.moveToNext());
                }

                for (Book book : list) {
                    System.out.println(book);
                }
            }
        });

        // 更新数据
        updateDataButton = (Button) findViewById(R.id.update_data);
        updateDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 10.99);
                db.update("Book", values, "name=?", new String[]{"Tom"});
                // update Book set price=10.99 where name='Tom'
            }
        });

        // 删除操作
        deleteDataButton = (Button) findViewById(R.id.delete_data);
        deleteDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages>?", new String[]{"20"});
                // delete from Book where pages>20
            }
        });
    }
}
